class_name SkillAfterDelaySys
extends System

const CATCH_READ = ["FrameTime", "SkillAfterDelay", "RoundFighter", "RoundStateC"]

var catch_read := []


func _init():
	system_name = "SkillAfterDelaySys"
	listen_modify(
		"on_delay_skill", "DelaySkillsC", ["FrameTime", "DelaySkillsC", "SkillAfterDelay"]
	)


func _system_ready():
	catch_read = world.query_manager.fetch_components(CATCH_READ)


#@listenModify(Multi<DelaySkillsC>)
func on_delay_skill(e, read: Array):
	var delay_skill = read[1].get(e.id) as DelaySkillsC
	if not delay_skill:
		return

	var frame = read[0].data as FrameTime
	var global_delay = read[2].data as SkillAfterDelay

	var delay = delay_skill.use_skill.skill.get(C.SkillAttrKey.castAfterDelay)
	delay = delay if delay else 0
	var next_time = delay_skill.use_skill.next_time
	delay = next_time + (delay / frame.frame_interval | 0)

	if global_delay.value < delay:
		global_delay.value = delay
